#ifndef _SBDNSE_H_
#define _SBDNS_H_

#include <stdio.h>
#include <unistd.h>
#include <ares.h>
#include "sbalance.h"
#include "sblist.h"
#include "sbutil.h"

#define SB_DNS_MAX_HOSTNAME_LEN 	256
#define SB_DNS_MAX_IP_LEN		64

#define SB_DNS_STATUS_DESTORYED		-1	/* not in wait or parse list and channel destoryed */
#define SB_DNS_STATUS_INITED		0	/* temp status, not in wait or parse list after inited */
#define SB_DNS_STATUS_WAITING		1	/* int wait list*/
#define SB_DNS_STATUS_PARSING		2	/* in parse list */
#define SB_DNS_STATUS_OK		3	/* parse ok, notice not in wait or parse and channel destoryed */
#define SB_DNS_STATUS_TIMEOUT		4	/* temp status */
#define SB_DNS_STATUS_ERROR		5	/* temp status */

#define SB_REQREPMSG_TYPESPLIT		10000

#define SB_DNS_STATUS_CHAR_OK		'0'
#define SB_DNS_STATUS_CHAR_FAIL		'1'
#define SB_DNS_STATUS_CHAR_TIMEOUT	'2'

#define SB_DNS_REQUEST_TYPE_QUERY	'0'
#define SB_DNS_REQUEST_TYPE_UPDATE	'1'

#define SB_DNS_BITMAP_LEN	SB_MAX_WORKERS_NUM >> 3 + 1 
#define SB_DNS_ARRAY_LEN	SB_MAX_WORKERS_NUM 

#if 0
struct msgbuf
{
	/* requeset msg type : worker id(start from one); response msg type : worker id + SB_REQREPMSG_TYPESPLIT */
  long mtype;	
	/* request: "host[#1]"; respose: "host#status#ip" */
	/* if request has '[#1]' means dns server should update host's ip cache */
  char mtext[SB_MSG_QUEUE_MAX_MSG_LEN];	
		
};

struct dns_msg
{
  int mtext_len;
  struct msgbuf m;
};
#endif

struct sb_dns
{
	char host[SB_DNS_MAX_HOSTNAME_LEN];
	char ip[SB_DNS_MAX_IP_LEN];	/* only save one ip address */
	
	int status;		/* 0:inited 1: parsing 2: done 3: timeout*/
	ares_channel channel;

	unsigned long timeout_time;
	struct list_head list_node;
	struct hlist_node hash_node;

	unsigned char waiting_dns_worker_bitmap[SB_DNS_BITMAP_LEN];
	int waiting_dns_worker_array[SB_DNS_ARRAY_LEN];	
	int waiting_dns_worker_num;
};

int sb_dns_init(int msgqueueid);
int sb_dns_main_loop(int msgqueueid);

#endif
